<template>
    <Header></Header>
    <!-- 主内容区域 -->
    <main class="container max-w-screen-xl mx-auto p-4">
        <!-- grid 表格布局，分为 4 列，元素间隔为 gap-4  -->
        <div class="grid grid-cols-4 gap-7">
            <!-- 左边栏，占用 3 列 -->
            <div class="col-span-4 md:col-span-3 mb-3">
                <!-- 文章列表，grid 表格布局，分为 2 列 -->
                <div class="grid grid-cols-2 gap-4">
                    <div class="col-span-2 md:col-span-1 animate__animated animate__fadeInUp" v-for="(article, index) in articleList" :key="index">
                        <div
                            class="relative bg-white h-full hover:scale-[1.03] border border-gray-200 rounded-lg dark:bg-gray-800 dark:border-gray-700">
                            <!-- 文章封面 -->
                            <a @click="goArticleDetailPage(article.id)" class="cursor-pointer">
                                <img class="rounded-t-lg h-48 w-full" :src="article.cover" alt="" />
                            </a>
                            <div class="p-5">
                                <!-- 标签 -->
                                <div class="mb-3">
                                    <span v-for="(tag, tagIndex) in article.tags" :key="tagIndex"
                                        @click="goTagArticlePageList(tag.id, tag.name)"
                                        class="cursor-pointer bg-green-100 text-green-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded hover:bg-green-200 hover:text-green-900 dark:bg-green-900 dark:text-green-300 dark:hover:bg-green-950">
                                        {{ tag.name }}
                                    </span>
                                </div>
                                <!-- 文章标题 -->
                                <a @click="goArticleDetailPage(article.id)">
                                    <h2 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">
                                        <span class="hover:border-gray-600 hover:border-b-2 dark:hover:border-gray-400">{{ article.title }}</span>
                                    </h2>
                                </a>
                                <!-- 文章摘要 -->
                                <p class="mb-3 font-normal text-gray-500 dark:text-gray-400">
                                    {{ article.summary }}
                                </p>
                                <!-- 文章发布时间、所属分类 -->
                                <p class="flex items-center font-normal text-gray-400 text-sm dark:text-gray-400">
                                    <!-- 发布时间 -->
                                    <svg class="inline w-3 h-3 mr-2 text-gray-400" aria-hidden="true"
                                        xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20">
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"
                                            stroke-width="2"
                                            d="M5 1v3m5-3v3m5-3v3M1 7h18M5 11h10M2 3h16a1 1 0 0 1 1 1v14a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1Z" />
                                    </svg>
                                    {{ article.createTime }}

                                    <!-- 所属分类 -->
                                    <svg class="inline w-3 h-3 ml-5 mr-2 text-gray-400"
                                        aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
                                        viewBox="0 0 18 18">
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"
                                            stroke-width="2"
                                            d="M1 5v11a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1V6a1 1 0 0 0-1-1H1Zm0 0V2a1 1 0 0 1 1-1h5.443a1 1 0 0 1 .8.4l2.7 3.6H1Z" />
                                    </svg>
                                    <a @click="goCategoryArticleList(article.category.id, article.category.name)"
                                        class="text-gray-400 hover:underline">
                                        {{ article.category.name }}
                                    </a>
                                </p>
                            </div>
                            <!-- 是否置顶 -->
                            <div v-if="article.isTop" class="absolute inline-flex items-center justify-center w-14 h-7 text-xs font-bold text-white bg-red-500 border-2 border-white rounded-full -top-2 -end-2 dark:border-gray-900">
                                置顶
                            </div>
                        </div>

                    </div>
                </div>
                <!-- 分页 -->
                <nav aria-label="Page navigation example" class="mt-10 flex justify-center">
                    <ul class="flex items-center -space-x-px h-10 text-base">
                        <li>
                            <a href="#" @click="getArticles(current - 1)"
                                class="flex items-center justify-center px-4 h-10 ml-0 leading-tight text-gray-500 bg-white border border-gray-300 rounded-l-lg hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
                                :class="[current > 1 ? '' : 'cursor-not-allowed']">
                                <span class="sr-only">Previous</span>
                                <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
                                    viewBox="0 0 6 10">
                                    <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"
                                        stroke-width="2" d="M5 1 1 5l4 4" />
                                </svg>
                            </a>
                        </li>
                        <li v-for="(pageOn, index) in pages" :key="index">
                            <a href="#" @click="getArticles(pageOn)"
                                class="flex items-center justify-center px-4 h-10 leading-tight border hover:bg-gray-100 hover:text-gray-700 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-600 dark:hover:text-white"
                                :class="[pageOn == current ? 'text-blue-600 bg-blue-50 border-blue-300 hover:bg-blue-100 hover:text-blue-700 dark:bg-gray-700' : 'text-gray-500 border-gray-300 bg-white hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800']">
                                {{ index + 1 }}
                            </a>
                        </li>
                        <li>
                            <a href="#" @click="getArticles(current + 1)"
                                class="flex items-center justify-center px-4 h-10 leading-tight text-gray-500 bg-white border border-gray-300 rounded-r-lg hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white"
                                :class="[current < pages ? '' : 'cursor-not-allowed']">
                                <span class="sr-only">Next</span>
                                <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none"
                                    viewBox="0 0 6 10">
                                    <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"
                                        stroke-width="2" d="m1 9 4-4-4-4" />
                                </svg>
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>

            <!-- 右边侧边栏，占用一列 -->
            <aside class="col-span-4 md:col-span-1 animate__animated animate__fadeInUp">
                <div class="sticky top-[5.5rem]">
                    <UserInfoCard></UserInfoCard>
                    <CategoryCard></CategoryCard>
                    <TagCard></TagCard>
                </div>
            </aside>
        </div>

    </main>
    <ScrollToTopButton></ScrollToTopButton>
    <!-- 页脚 -->
    <Footer></Footer>
</template>

<script setup>
import Header from './components/Header.vue';
import Footer from './components/Footer.vue';
import ScrollToTopButton from './components/ScrollToTopButton.vue';
import { ref, onMounted } from 'vue';
import { getArticlePageList } from '@/api/frontend/article';
import { showMessage } from '@/composables/utils';
import UserInfoCard from './components/UserInfoCard.vue';
import CategoryCard from './components/CategoryCard.vue';
import TagCard from './components/TagCard.vue';
import { useRouter } from 'vue-router';

const router = useRouter()

//文章集合
const articleList = ref([])
// 当前页码
const current = ref(1)
// 每页显示文章数
const size = ref(10)

// 分页数据获取
// 总文章数
const total = ref(0)
// 总页数
const pages = ref(0)

// 获取指定页的文章数据
function getArticles(currentOn) {
    if (currentOn < 1 || (pages.value > 0 && currentOn > pages.value)) return
    // 调用接口获取文章列表
    // 请求参数
    let params = {
        "current": currentOn,
        "size": size.value
    }
    getArticlePageList(params).then((res) => {
        if (res.success == true) {
            articleList.value = res.data
            current.value = res.pageIndex
            size.value = res.size
            total.value = res.total
            pages.value = res.pages
        } else {
            let message = res.message
            showMessage(message, 'error')
        }
    })
}
// 初始化
getArticles(current.value)

// 分类跳转
const goCategoryArticleList = (id, name) => {
    router.push({path: '/category/article/list', query: {id: id, name: name}})
}
// 标签跳转
// 跳转到标签文章列表页
const goTagArticlePageList = (id, name) => {
    router.push({path: '/tag/article/list', query: {id: id, name: name}})
}
// 跳转到文章详情页
const goArticleDetailPage = (id) => {
    router.push({path: '/article/detail/' + id})
}
</script>